package com.xcity.game.employee.config.impl;

import java.util.ArrayList;
import java.util.List;

import com.xcity.game.combat.attr.CombatAttribute;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.employee.config.AbstractConfigure;
import com.xcity.game.employee.config.Configure;
import com.xcity.game.employee.template.EmployeeBlessTemplate;
import com.xcity.util.ExpCalculator;

import gnu.trove.map.hash.TIntObjectHashMap;
import naga.x.App;
import naga.x.game.template.TemplateService;

public class EmployeeBlessConfigure extends AbstractConfigure {

	private TIntObjectHashMap<List<EmployeeBlessTemplate>> blessId2templist = new TIntObjectHashMap<List<EmployeeBlessTemplate>>();
	private TIntObjectHashMap<ExpCalculator> blessId2calc = new TIntObjectHashMap<ExpCalculator>();
	public EmployeeBlessConfigure(Configure parent) {
		super(parent);
	}

	@Override
	public EmployeeBlessConfigure configure() {
		List<EmployeeBlessTemplate> list = App.getApp().getServiceManager().get(TemplateService.class).getTemplatesList(EmployeeBlessTemplate.class);
		int size = list.size();
		for(int i = 0; i < size; i++){
			EmployeeBlessTemplate temp = list.get(i);
			ExpCalculator calc;
			if(!blessId2calc.containsKey(temp.getBlessId())){
				calc = new ExpCalculator(120);
				blessId2calc.put(temp.getBlessId(), calc);
			}else{
				calc = blessId2calc.get(temp.getBlessId());
			}
			calc.setAndSum(temp.getLevel(), temp.getNeedExp());
			List<EmployeeBlessTemplate> tmpList;
			if(!blessId2templist.containsKey(temp.getBlessId())){
				tmpList = new ArrayList<EmployeeBlessTemplate>();
				blessId2templist.put(temp.getBlessId(), tmpList);
			}else{
				tmpList = blessId2templist.get(temp.getBlessId());
			}
			tmpList.add(temp);
		}
		
		int[] blessIds = blessId2templist.keys();
		for(int i = 0, len = blessIds.length; i < len; i++){//blessid
			List<EmployeeBlessTemplate> tempList = blessId2templist.get(blessIds[i]);
			int count = tempList.size();
			if(count > 0){
//				CombatAttribute[] totalAtts = attrs.toArray(new CombatAttribute[attrs.size()]);
				List<CombatAttribute> totalAtts = new ArrayList<CombatAttribute>();
				for(int j = 0; j < count; j++){//100
					CombatAttribute[] currentAtts = tempList.get(j).getAttributes();
					for(int k = 0, ksize = currentAtts.length; k < ksize; k++){//1~2
						boolean needNew = true;
						for(int l = 0, lsize = totalAtts.size(); l < lsize; l++){//1~2
							if(totalAtts.get(l).getType() == currentAtts[k].getType()){
								totalAtts.get(l).setValue(totalAtts.get(l).getValue() + currentAtts[k].getValue());
								needNew = false;
								break;
							}
						}
						if(needNew){
//							CombatAttribute newAtt = new CombatAttribute(currentAtts[k].getType(), 0f);
							totalAtts.add(currentAtts[k].clone());
						}
					}
					CombatAttribute[] curTotal = new CombatAttribute[totalAtts.size()];
					for(int k = 0, ksize = totalAtts.size(); k < ksize; k++){
						curTotal[k] = totalAtts.get(k).clone();
					}
					tempList.get(j).putTotalAtts(curTotal);//totalAtts.toArray(new CombatAttribute[totalAtts.size()])
				}
			}
		}
		return this;
	}
	public TIntObjectHashMap<List<EmployeeBlessTemplate>> getBlessId2TempList() {
		return blessId2templist;
	}
	public TIntObjectHashMap<ExpCalculator> getBlessId2calc() {
		return blessId2calc;
	}
	
	public EmployeeBlessTemplate getTempByLvlBlessId(int blessId, int lvl){
		List<EmployeeBlessTemplate> list = blessId2templist.get(blessId);
		if(list != null){
			int size = list.size();
			int index = lvl - 1;
			if(index > -1 && index < size){
				return list.get(index);
			}
		}
		return null;
	}
}
